package com.eigaland.change_machine.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
import android.widget.Chronometer
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.MutableLiveData
import com.eigaland.change_machine.R
import kotlinx.android.synthetic.main.dialog_take_ticket.view.*
import kotlinx.android.synthetic.main.fragment_login.*
import movie6.posprintercept2.extensions.textWatcher
import kotlin.math.roundToInt

public class TakeTicketDialog : DialogFragment() {
    var time = 60

    var leftClickLiveData = MutableLiveData<String>()

    var rightClickLiveData = MutableLiveData<String>()


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            var view = inflater.inflate(R.layout.dialog_take_ticket,null)


            dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
              dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
               var attr = dialog?.window?.attributes
                attr?.windowAnimations = R.style.BottomDialogAnimation
                dialog?.window?.attributes = attr

        view?.ed_input_ticket_code?.textWatcher {
            this.onTextChanged { charSequence, i1, i2, i3 ->
                charSequence?.let {
                    view?.btn_right?.isEnabled = it.isNotEmpty()
                }

            }

        }

        view?.tv_chronometer?.visibility = View.VISIBLE
        view?.tv_chronometer?.start()
        view?.tv_chronometer?.setOnChronometerTickListener {
            time -= 1
            it.text = "" + time + "s"
            if(time == 0) dismiss()

        }
        view?.btn_left?.setOnClickListener {
            dismiss()
            leftClickLiveData.postValue("")
        }
        view?.btn_right?.setOnClickListener {
            rightClickLiveData.postValue(view.btn_right?.text.toString())
        }

           return view
    }

    fun showSuccessStatus(){
        view?.btn_right?.text = "再次打印"
        view?.btn_right?.visibility = View.VISIBLE
        view?.btn_left?.visibility = View.VISIBLE
        view?.ll_left_btn_area?.visibility = View.VISIBLE
        view?.tv_take_ticket_loading?.visibility = View.GONE
        view?.tv_take_ticket_success?.visibility = View.VISIBLE
    }

    fun showTakeStatus(ticketCount:Int){

        if(ticketCount > 0) {
            view?.tv_take_ticket_loading?.text = "正在打印第" + ticketCount + "张电影票..."
        }

        view?.btn_right?.visibility = View.GONE
        view?.tv_dialog_title?.visibility = View.GONE
        view?.tv_take_ticket_loading?.visibility = View.VISIBLE
        view?.btn_left?.visibility = View.GONE
        view?.ll_left_btn_area?.visibility = View.GONE

        view?.ll_input_area?.visibility = View.GONE


    }

    fun showErrorMsg(errorMsg:String){
        view?.btn_right?.visibility = View.GONE
        view?.tv_dialog_title?.visibility = View.GONE
        view?.tv_take_ticket_loading?.visibility = View.VISIBLE
        view?.btn_left?.visibility = View.GONE
        view?.ll_left_btn_area?.visibility = View.GONE

        view?.ll_input_area?.visibility = View.GONE

        view?.tv_take_ticket_loading?.text = errorMsg

    }

    fun resetStatus(){

        view?.btn_right?.text = "确定打印"
        view?.tv_dialog_title?.visibility = View.VISIBLE
        view?.btn_right?.visibility = View.VISIBLE
        view?.btn_left?.visibility = View.VISIBLE
        view?.ll_left_btn_area?.visibility = View.VISIBLE
        view?.ll_input_area?.visibility = View.VISIBLE

        view?.tv_take_ticket_loading?.visibility = View.GONE
        view?.tv_take_ticket_success?.visibility = View.GONE


    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, tag)

    }

    override fun onResume() {
        super.onResume()
        var displayMetrics = DisplayMetrics()
        var manager = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        manager.defaultDisplay.getMetrics(displayMetrics)
        dialog?.window?.setLayout((displayMetrics.widthPixels * 0.5).roundToInt(),(displayMetrics.heightPixels * 0.7).roundToInt())
    }


}


